import { Toast } from 'react-vant'
const initialState = {
    swiper:[],  //轮播图数据
    list:[],
    sonList:[],  //子数据
    Details:[], //详情数据
    shopCart:[], //购物车
    Collection:[] //收藏
}

export default (state = initialState, { type, payload }) => {
    const newState =JSON.parse(JSON.stringify(state));
switch (type) {

  case "GET_SWIPER":
      newState.swiper =payload;
    return newState
  case "GET_LIST":
      newState.list =payload;
      newState.sonList=newState.list[0].children
    return newState
    
  case "C_INDEX":
      newState.sonList = newState.list[payload].children;
      window.localStorage.setItem('sonList',JSON.stringify(newState.sonList));
    return newState 
  case "GET_DETAIL":
      newState.sonList =JSON.parse(window.localStorage.getItem('sonList'))
      newState.Details = newState.sonList.filter((item) => {
        return item.id == payload
      })
   case "ADD_CART":
     console.log()
    newState.sonList.forEach((item,index) => {
        if(item.id == payload){
            newState.shopCart.push(item)
        }
    });
    window.localStorage.setItem('shopCart',JSON.stringify(newState.shopCart));  
    return newState 

  case "SET_LIKE":
    newState.sonList.forEach((item,index) => {
      if(item.id == payload){
        item.trag = !item.trag
        Toast.success({message:item.trag ? "收藏成功":"取消收藏"})
      }
  });
    return newState  

  case "GET_SHOP":
    return newState  
  default:
    return newState
  }
}
